package com.arpa.qimen.cainiao.websocket;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;

public class WebSocketClientManager extends WebSocketClient {
    static WebSocketClientManager webSocket = null;

    public static void main(String[] args) throws URISyntaxException {
        String uri = "ws://127.0.0.1:13528";
        webSocket = new WebSocketClientManager(new URI(uri), new Draft_17());
        //建立连接
        webSocket.connect();
        webSocket.onOpen(new ServerHandshake() {
            @Override
            public short getHttpStatus() {
                return 0;
            }

            @Override
            public String getHttpStatusMessage() {
                return null;
            }

            @Override
            public Iterator<String> iterateHttpFields() {
                return null;
            }

            @Override
            public String getFieldValue(String s) {
                return null;
            }

            @Override
            public boolean hasFieldValue(String s) {
                return false;
            }

            @Override
            public byte[] getContent() {
                return new byte[0];
            }
        });
    }

    public WebSocketClientManager(URI serverUri, Draft draft) {
        super(serverUri, draft);
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        //获取打印机列表
        String getPrinterListCmd = "{\"requestID\":\"1234567890\",\"verson\":\"1.0\",\"cmd\":\"getPrinters\"}";
        webSocket.send(getPrinterListCmd);

        //发送打印任务
        String printCmd = "{\"cmd\": \"print\",\"requestID\": \"123458976\",\"version\": \"1.0\",\"task\": {\"taskID\": \"7293666\",\"preview\": false,\"printer\": \"\"," +
                "\"notifyMode\": \"allInOne\",\"previewType\": \"pdf\",\"documents\": [{\"documentID\": \"0123456789\",\"contents\": [" +
                "{\"data\": {\"recipient\": {\"address\": {\"city\": \"杭州市\",\"detail\": \"良睦路999号乐佳国际大厦2号楼小邮局\"," +
                "\"district\": \"余杭区\",\"province\": \"浙江省\",\"town\": \"\"},\"mobile\": \"13012345678\"," +
                "\"name\": \"菜鸟网络\",\"phone\": \"057112345678\"},\"routingInfo\": {\"consolidation\": {" +
                "\"name\": \"杭州\",\"code\": \"hangzhou\"},\"origin\": {\"name\": \"杭州\",\"code\": \"POSTB\"" +
                "},\"sortation\": {\"name\": \"杭州\"},\"routeCode\": \"123A-456-789\"},\"sender\": {" +
                "\"address\": {\"city\": \"杭州市\",\"detail\": \"文一西路1001号阿里巴巴淘宝城5号小邮局\",\"district\": \"余杭区\"," +
                "\"province\": \"浙江省\",\"town\": \"\"},\"mobile\": \"13012345678\",\"name\": \"阿里巴巴\",\"phone\": \"057112345678\"" +
                "},\"shippingOption\": {\"code\": \"COD\",\"services\": {\"SVC-COD\": {\"value\": \"200\"},\"TIMED-DELIVERY\": {\"value\": \"SEVERAL-DAYS\"},\"PAYMENT-TYPE\": {\"value\": \"ON-DELIVERY\"},\"SVC-INSURE\": {\"value\": \"1000000\"},\"SVC-PROMISE-DELIVERY\": {\"promise-type\": \"SAMEDAY_DELIVERY\"}},\"title\": \"代收货款\"},\"waybillCode\": \"0123456789\"},\"signature\": \"19d6f7759487e556ddcdd3d499af087080403277b7deed1a951cc3d9a93c42a7e22ccba94ff609976c5d3ceb069b641f541bc9906098438d362cae002dfd823a8654b2b4f655e96317d7f60eef1372bb983a4e3174cc8d321668c49068071eaea873071ed683dd24810e51afc0bc925b7a2445fdbc2034cdffb12cb4719ca6b7\"," +
                "\"templateURL\": \"http://cloudprint.cainiao.com/cloudprint/template/getStandardTemplate.json?template_id=101&version=4\"" +
                "}]}]}}";
        webSocket.send(printCmd);
    }

    // WebSocket回调函数
    @Override
    public void onMessage(String message) {
        //TODO 对打印服务返回的数据进行处理
        System.out.println(message);
    }

    @Override
    public void onClose(int i, String s, boolean b) {

    }

    @Override
    public void onError(Exception e) {

    }
}
